package orgs.example.controller;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import orgs.example.pojo.MeetingRoom;
import orgs.example.pojo.Reservation;
import orgs.example.pojo.Result;
import orgs.example.pojo.User;
import orgs.example.service.AdminService;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
@Validated
public class AdminController {

    @Autowired
    private AdminService adminService;


    @PostMapping("/addroom")
    public Result addroom(@RequestBody MeetingRoom meetingRoom){
        meetingRoom.setRoomStatus("空闲");
        Boolean is = adminService.addMeetingRoom(meetingRoom);
        if(is)
            return Result.success("添加成功");
        else{
            return Result.error("添加失败");
        }
    }

    @PatchMapping("/changeroom")
    public Result changeroom(@RequestBody MeetingRoom meetingRoom){

        Boolean is = adminService.changeMeetingroom(meetingRoom);
        if (is)
            return Result.success("修改成功");
        else{
            return Result.error("修改失败");
        }
    }

    @PostMapping("/deleteroom")
    public Result deleteroom( @RequestBody Map<String,Object> params){
        Integer roomId = (Integer) params.get("roomId");
        Boolean is = adminService.deleteroom(roomId);
        if(is)
            return Result.success("删除成功");
        else{
            return Result.success("删除失败");
        }
    }

    @PostMapping("changeuserstatus")
    public Result changeuserstatus(@RequestBody Map<String,Object> params){
        Integer userId = (Integer) params.get("userId");
        String userStatus = (String) params.get("userStatus");

        Boolean is = adminService.changeUserStatus(userId,userStatus);
        if(is)
            return Result.success("修改成功");
        else{
            return Result.error("修改失败");
        }
    }


    @PostMapping("/deleteuserstatus")
    public Result deleteuserstatus(@RequestBody Map<String,Object> params){
        Integer userId = (Integer) params.get("userId");
        adminService.deleteuser(userId);

        adminService.deleteCancellationByuserId(userId);

        adminService.deleteReservaionByUserId(userId);


        return  Result.success("删除成功");

    }

    @GetMapping("/searchuserstatus")
    public Result<Object> searchuserstatus(){

        User user = new User();

        List<User> lists = adminService.searchUser(user);

        return Result.success("查询成功",lists);
    }

    @PostMapping("/searchroom")
    public Result<Object> searchroom(@RequestBody MeetingRoom meetingRoom){

        Date now = new Date();

        List<Reservation> lists = adminService.searchAllReservation();

        Iterator<Reservation> iterator = lists.iterator();
        while(iterator.hasNext()){
            Reservation r = iterator.next();

            Date reserveStart = r.getReserveStart();
            Date reserveEnd = r.getReserveEnd();

            Integer roomid = r.getRoomId();

            if(now.before(reserveEnd) && now.after(reserveStart) && r.getReserveStatus().equals("已支付")){
                adminService.changeRoomStatus(roomid,"使用");
            }
        }

        List<MeetingRoom> rooms = adminService.searchMeetingRoom(meetingRoom);

        return Result.success("查询成功",rooms);

    }

    @PostMapping("/getroom")
    public Result<Object> getroom(@RequestBody Map<String,Object> params){
        Integer roomId = (Integer) params.get("roomId");

        Date now = new Date();

        List<Reservation> lists = adminService.searchAllReservation();

        Iterator<Reservation> iterator = lists.iterator();
        while(iterator.hasNext()){
            Reservation r = iterator.next();

            Date reserveStart = r.getReserveStart();
            Date reserveEnd = r.getReserveEnd();

            Integer roomid = r.getRoomId();

            if(now.before(reserveEnd) && now.after(reserveStart) && r.getReserveStatus().equals("已支付")){
                adminService.changeRoomStatus(roomid,"使用");
            }
        }


        MeetingRoom room = adminService.searchRoomByRoomId(roomId);

        return Result.success("查询成功",room);
    }


}
